package com.sh.data.engine.domain.base.model.datax;

import com.sh.data.engine.common.enumDefinition.DSType;
import com.sh.data.engine.domain.base.model.entity.TbTaskFieldMapEntity;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;

/**
 * @auther: futang.tyf
 * @date: 2018/10/30 17:28
 * @description:
 */
@Data
@Slf4j
public class DataX implements Serializable {

    private Job job;

    private Core core;

    @Data
    @Builder
    public static class Core implements Serializable {

        private Transport transport;

    }

    @Data
    @Builder
    public static class Transport implements Serializable {
        private Channel channel;
    }


    @Data
    @Builder
    public static class Channel implements Serializable {
        private Speed speed;
    }

    @Data
    @Builder
    public static class Speed implements Serializable {
        private Integer record;
    }

    @Data
    public static class Job implements Serializable {

        private JobSetting setting;

        private List<JobContent> content;

        public Job buildTextReader(
            List<String> filePaths,
            List<TbTaskFieldMapEntity> fieldMappings,
            Integer skipCount,
            String encodeFormat) {
            JobContent content = this.content.get(0);
            JobContentReader reader = new JobContentReader();
            reader.setName(DataxReaderAndWriter.getReaderByName(DSType.Text.name()));

            TextReaderParameter parameter = new TextReaderParameter();
            parameter.setPath(filePaths);
            parameter.setColumnsNew(fieldMappings);
            // 写入参数：首行是否跳过
            parameter.setSkipCount(skipCount);

            // csv格式的文件要用 GBK/UTF-8 编码读取，否则会有乱码
            if (filePaths.get(0).endsWith(".csv")) {
                parameter.setEncoding(encodeFormat);
            }

            reader.setTextParameter(parameter);
            content.setReader(reader);

            return this;
        }
    }

    @Data
    public static class JobSetting implements Serializable {

        private JobSettingSpeed speed;

        private JobSettingErrorLimit errorLimit;
    }

    @Data
    public static class JobSettingSpeed implements Serializable {

        private Integer channel;

        private Integer record;
    }

    @Data
    public static class JobSettingErrorLimit implements Serializable {

        private Integer record;
    }

    @Data
    public static class JobContent implements Serializable {

        private JobContentReader reader;

        private JobContentWriter writer;
    }

    @Data
    public static class JobContentReader implements Serializable {

        private String name;

        private JobContentReaderParameter parameter;

        private TextReaderParameter textParameter;

        private MaxComputeReaderParameter maxComputeReaderParameter;
    }

    @Data
    public static class JobContentWriter implements Serializable {

        private String name;

        private JobContentWriterParameter parameter;
    }

    public static String getDsTypeByWriterName(String writerName) {
        String dsType = null;
        if (DataxReaderAndWriter.getWriterByName("Oracle").equalsIgnoreCase(writerName)) {
            dsType = "ORACLE";
        } else if (DataxReaderAndWriter.getWriterByName("MySQL").equalsIgnoreCase(writerName)
            || DataxReaderAndWriter.getWriterByName("MySQL8").equalsIgnoreCase(writerName)) {
            dsType = "MYSQL";
        } else if (DataxReaderAndWriter.getWriterByName("SQLServer").equalsIgnoreCase(writerName)) {
            dsType = "SQLServer";
        } else if (DataxReaderAndWriter.getWriterByName("PostgreSQL").equalsIgnoreCase(writerName)) {
            dsType = "PostgreSQL";
        } else if ("rdbmswriter".equalsIgnoreCase(writerName)) {
            dsType = "sap,DB2,DM,KINGBASE8";
        } else if (DataxReaderAndWriter.getWriterByName("Hive").equalsIgnoreCase(writerName)) {
            dsType = "Hive";
        }
        return dsType;
    }

    public static String getDsTypeByReaderName(String readerName) {
        String dsType = null;
        if (DataxReaderAndWriter.getReaderByName("Oracle").equalsIgnoreCase(readerName)) {
            dsType = "ORACLE";
        } else if (DataxReaderAndWriter.getReaderByName("MySQL").equalsIgnoreCase(readerName)
            || DataxReaderAndWriter.getReaderByName("MySQL8").equalsIgnoreCase(readerName)) {
            dsType = "MYSQL";
        } else if (DataxReaderAndWriter.getReaderByName("SQLServer").equalsIgnoreCase(readerName)) {
            dsType = "SQLServer";
        } else if (DataxReaderAndWriter.getReaderByName("PostgreSQL").equalsIgnoreCase(readerName)) {
            dsType = "PostgreSQL";
        } else if ("rdbmswriter".equalsIgnoreCase(readerName)) {
            dsType = "sap,DB2,DM,KINGBASE8";
        } else if (DataxReaderAndWriter.getWriterByName("Hive").equalsIgnoreCase(readerName)) {
            dsType = "Hive";
        }
        return dsType;
    }
}
